package com.huopaolan.yohuoshop.Mvp.Presenter;

import com.huopaolan.lib_core.Mvp.Presenter.BasePresenter;
import com.huopaolan.yohuoshop.Bean.ShowEntity;
import com.huopaolan.yohuoshop.Mvp.Contract.MyFragmentContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MyFragmentPresenter extends BasePresenter<MyFragmentContract.MyIView, MyFragmentContract.MyIModel> {
    @Inject
    public MyFragmentPresenter(MyFragmentContract.MyIView myIView, MyFragmentContract.MyIModel myIModel) {
        super(myIView, myIModel);
    }

    /**
     * 数据请求
     */
    public void MyRequest() {
        JSONObject show = new JSONObject();
        try {
            show.put("page", "1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String request = show.toString();
        m.my_request(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ShowEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ShowEntity showEntity) {
                        buildData(showEntity);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void buildData(ShowEntity showEntity) {
        //banner集合
//        List<ShowEntity.Banner> bannerList = buildBanner(showEntity);
        //brand集合
//        List<ShowEntity.Brand> brandList = buildBrand(showEntity);
        //values集合
        List<ShowEntity.Values> valuesList = buildValues(showEntity);
        Map<String, List> map = new HashMap<>();
//        map.put("bannerList", bannerList);
//        map.put("brandList", brandList);
        map.put("valuesList", valuesList);
        v.myShopSuccess(map);
    }

    private List<ShowEntity.Values> buildValues(ShowEntity entity) {
        List<ShowEntity.Values> values = new ArrayList<>();
        for (int i = 0; i < entity.values.size(); i++) {
            if (Integer.parseInt(entity.values.get(i).goods_id) >= 1 && Integer.parseInt(entity.values.get(i).goods_id) <= 10) {
                ShowEntity.Values valueslist = new ShowEntity.Values();
                valueslist.user_id = entity.values.get(i).user_id;
                valueslist.user_head = entity.values.get(i).user_head;
                valueslist.detail_flag = entity.values.get(i).detail_flag;
                valueslist.nick_name = entity.values.get(i).nick_name;
                valueslist.show_id = entity.values.get(i).show_id;
                valueslist.goods_discount_price = entity.values.get(i).goods_discount_price;
                valueslist.img_path = entity.values.get(i).img_path;
                valueslist.goods_id = entity.values.get(i).goods_id;
                valueslist.goods_img_path = entity.values.get(i).goods_img_path;
                valueslist.goods_name = entity.values.get(i).goods_name;
                valueslist.fabulous = entity.values.get(i).fabulous;
                valueslist.goods_img_id = entity.values.get(i).goods_img_id;
                values.add(valueslist);
            }
        }
        return values;
    }

//    private List<ShowEntity.Brand> buildBrand(ShowEntity entity) {
//        List<ShowEntity.Brand> brands = new ArrayList<>();
//        for (int i = 0; i < entity.brand.size(); i++) {
//            if (Integer.parseInt(entity.brand.get(i).brand_id) >= 1 && Integer.parseInt(entity.brand.get(i).brand_id) <= 8) {
//                ShowEntity.Brand brand = new ShowEntity.Brand();
//                brand.brand_name = entity.brand.get(i).brand_name;
//                brand.brand_icon = entity.brand.get(i).brand_icon;
//                brand.brand_id = entity.brand.get(i).brand_id;
//                brand.menu_id = entity.brand.get(i).menu_id;
//                brands.add(brand);
//            }
//        }
//        return brands;
//    }

    private List<ShowEntity.Banner> buildBanner(ShowEntity entity) {
        List<ShowEntity.Banner> banners = new ArrayList<>();
        for (int i = 0; i < entity.banner.size(); i++) {
            if (Integer.parseInt(entity.banner.get(i).recommend_id) >= 1 && Integer.parseInt(entity.banner.get(i).recommend_id) <= 7) {
                ShowEntity.Banner banner = new ShowEntity.Banner();
                banner.recommend_id = entity.banner.get(i).recommend_id;
                banner.recommend_name = entity.banner.get(i).recommend_name;
                banner.recommend_path = entity.banner.get(i).recommend_path;
                banner.recommend_url = entity.banner.get(i).recommend_url;
                banners.add(banner);
            }
        }
        return banners;
    }
}
